<?
require_once(dirname(__FILE__) . '/../../classes/report_interface.inc.php');


class LoggerModuleMainReport implements LoggerReportInterface
{
    protected $database;

    public function __construct(LoggerDatabase $database) 
    {
        $this->database = $database;
    }

    public function GetReport($root, $date1, $date2, $params = false)
    {
        $xml = $root->ownerDocument;

        $name = $xml->createElement('report_name', 'Сводная статистика');
        $root->appendChild($name);

        $arReport = LoggerConfig::$modules['day']['reports']['main'];
        require_once(dirname(__FILE__) . '/../..' . $arReport['src']);
        $classReport = new $arReport['class']($this->database);
        $dayData = $classReport->GetData($date1, $date2);
    
        $arReport = LoggerConfig::$modules['sources']['reports']['main'];
        require_once(dirname(__FILE__) . '/../..' . $arReport['src']);
        $classReport = new $arReport['class']($this->database);
        $linksData = $classReport->GetLinksData($date1, $date2);
        $searchData = $classReport->GetSearchData($date1, $date2);
    
        $arReport = LoggerConfig::$modules['robots']['reports']['main'];
        require_once(dirname(__FILE__) . '/../..' . $arReport['src']);
        $classReport = new $arReport['class']($this->database);
        $robotsData = $classReport->GetData($date1, $date2, array(), 0, 1000);        

        $dbDataLength = 0;
        $dbIndexLength = 0;
        $SelectSQL = 'SHOW TABLE STATUS FROM `'.mysql_escape_string(LoggerConfig::$mysqlDatabase).'` ';
        $SelectSQL .= 'WHERE `name` LIKE \''.mysql_escape_string(LoggerConfig::$mysqlTablePrefix).'%\'';
        $result = $this->database->Query($SelectSQL);
        while ($result && ($row = $this->database->FetchAssoc($result)))
        {
            $dbDataLength += $row['Data_length'];
            $dbIndexLength += $row['Index_length'];
        }

        $dayElement = $xml->createElement('day');
        $root->appendChild($dayElement);
        $dayElement->setAttribute('hits', intval($dayData['hits']));
        $dayElement->setAttribute('sessions', intval($dayData['sessions']));
        $dayElement->setAttribute('visitors', intval($dayData['visitors']));
        $dayElement->setAttribute('visitorsNew', intval($dayData['visitorsNew']));
    
        $sourcesElement = $xml->createElement('sources');
        $root->appendChild($sourcesElement);
        $sourcesElement->setAttribute('servers', intval($linksData));
        $sourcesElement->setAttribute('engines', intval($searchData));
    
        $hits = 0;
        foreach($robotsData as $row)
            $hits += $row['summ'];
        $robotsElement = $xml->createElement('robots');
        $root->appendChild($robotsElement);
        $robotsElement->setAttribute('count', count($robotsData));
        $robotsElement->setAttribute('hits', $hits);

        $dbElement = $xml->createElement('db');
        $root->appendChild($dbElement);
        $dbElement->setAttribute('data', round($dbDataLength / 1048576, 1));
        $dbElement->setAttribute('index', round($dbIndexLength / 1048576, 1));

        $xsl = dirname(__FILE__) . '/templates/main.xsl';
        $pi = $xml->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$xsl.'"');
        $xml->insertBefore($pi, $root);
        return true;
    }
}
